package com.phamloc.shootingchicken.extra;

import java.io.IOException;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Set;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.music.MusicManager;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.audio.sound.SoundManager;

import android.content.Context;
import android.util.Log;

public class AudioPlayer {
	private static final String TAG = "AudioPlayer";
	private final EnumMap<SoundKeys, Sound> mSounds;
	private final EnumMap<MusicKeys, Music> mMusic;
	private final Set<SoundKeys> mPausedSounds;
	private final Set<MusicKeys> mPausedMusic;
	private boolean mMusicOn = true;
	private boolean mSoundsOn = true;
	public static enum SoundKeys{
		HEN_SHOT,
		BUTTON_PRESS
	};
	public static enum MusicKeys{
		GAME_MUSIC
	};
	public AudioPlayer(final boolean pMusicOn, final boolean pSoundOn) {
		mMusicOn = pMusicOn;
		mSoundsOn = pSoundOn;
		mSounds = new EnumMap<SoundKeys, Sound>(SoundKeys.class);;
		mMusic = new EnumMap<MusicKeys, Music>(MusicKeys.class);;
		mPausedSounds = new HashSet<SoundKeys>();
		mPausedMusic = new HashSet<MusicKeys>();
	}

	public void loadMusicAndSound(final SoundManager pSoundManager, final MusicManager pMusicManager, final Context pContext) throws IOException {
		try {
			loadMusic(pMusicManager, pContext);
			loadSoundEffects(pSoundManager, pContext);
		} catch (final IOException e) {
			Log.e(TAG, "Couldn't load audio asset.", e);
			throw e;
		}
	}

	public void play(final MusicKeys pMusic) {
		playAudio(getAudio(pMusic, mMusic), mMusicOn, false);
	}

	private void playAudio(Music audio, boolean mMusicOn, boolean b) {
		if(mMusicOn && audio != null){
			audio.play();
			audio.setVolume(1f);
		}else{
			audio.pause();
		}
	}

	private void playAudio(Sound audio, boolean mSoundsOn, boolean b) {
		if(mSoundsOn  && audio != null){
			audio.play();
			audio.setVolume(1f);
		}else{
			audio.pause();
		}
	}

	private Music getAudio(MusicKeys pMusic, EnumMap<MusicKeys, Music> mMusic) {
		return mMusic.get(pMusic);
	}

	public void play(final SoundKeys pSound) {
		playAudio(getAudio(pSound, mSounds), mSoundsOn, false);
	}	

	private Sound getAudio(SoundKeys pSound, EnumMap<SoundKeys, Sound> mSounds) {
		return mSounds.get(pSound);
	}

	private void loadMusic(final MusicManager musicManager, final Context context) throws IOException {
		MusicFactory.setAssetBasePath("sound/");
		final Music gameMusic = MusicFactory.createMusicFromAsset(musicManager, context, "game_music.mp3");
		gameMusic.setLooping(true);
		gameMusic.setVolume(0f);
		mMusic.put(MusicKeys.GAME_MUSIC, gameMusic);
	}

	private void loadSoundEffects(final SoundManager soundManager, final Context context) throws IOException {
		SoundFactory.setAssetBasePath("sound/");
		final Sound henShoot = SoundFactory.createSoundFromAsset(soundManager, context, "hen_shot.wav");
		mSounds.put(SoundKeys.HEN_SHOT, henShoot);
	}	
}